package com.turnengine.client.global.game.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.bean.GameDefinitionSerializer;
import com.turnengine.client.global.game.bean.IGameDefinition;
import java.io.IOException;

/**
 * The Get Game Definition Data Serializer.
 */
public class GetGameDefinitionDataSerializer extends AbstractCommandDataSerializer<IGameDefinition> implements IGetGameDefinitionDataSerializer {

	/**
	 * Creates a new Get Game Definition Data Serializer.
	 */
	public GetGameDefinitionDataSerializer() {
		super(GetGameDefinition.COMMAND_ID);
	}

	@Override
	public IGameDefinition readReturnValue(IDataReader reader) throws IOException {
		IGameDefinition value = new GameDefinitionSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGameDefinition value) throws IOException {
		new GameDefinitionSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGameDefinition> readRequest(IDataReader reader) throws IOException {

		// Read fields
		int instanceId = reader.readInt();

		// Create object
		GetGameDefinition object = new GetGameDefinition();
		object.setInstanceId(instanceId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGameDefinition> argument) throws IOException {
		GetGameDefinition object = (GetGameDefinition) argument;

		// Get fields
		int instanceId = object.getInstanceId();

		// Write fields
		writer.writeInt(instanceId);
	}

	@Override
	public IExecutableCommandResponse<IGameDefinition> newResponse() {
		return new GetGameDefinitionResponse();
	}
}
